package org.ggumm21.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.ggumm21.moneybook.BookManager;

public class DateUtil {

	static public String getYm(Date d) {
		return new SimpleDateFormat("yyyyMM").format(d);
	}
	
	static public String getNextYm(String ym) {
		int yy = Integer.parseInt(ym.substring(0, 4));
		int mm = Integer.parseInt(ym.substring(4), 10);
		if (mm == 12) {
			mm = 1;
			yy++;
		} else {
			mm++;
		}
		StringBuffer sb = new StringBuffer();
		sb.append(yy);
		if (mm < 10) sb.append("0");
		sb.append(mm);
		return sb.toString();
	}
	
	static public String getPrevYm(String ym) {
		return getPrevYm(ym, 1);
	}
	
	static public String getPrevYm(String ym, int gap) {
		if (gap == 0) return ym;
		int yy = Integer.parseInt(ym.substring(0, 4));
		int mm = Integer.parseInt(ym.substring(4), 10);
		
		int mm1 = 12 - (Math.abs(gap - mm + 12) % 12);
		int yy1 = (int) (yy + Math.ceil((double)(mm - gap)/12)-1);
		
		StringBuffer sb = new StringBuffer();
		
		sb.append(yy1);
		
		if (mm1 < 10) sb.append("0");
		sb.append(mm1);
		
		return sb.toString();
	}

	public static Calendar getCalendar(String ym, int day) {
	
		Calendar cal = Calendar.getInstance();
		int yy = Integer.parseInt(ym.substring(0, 4));
		int mm = Integer.parseInt(ym.substring(4), 10);
		cal.set(yy, mm-1, day);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		
		return cal;
	}
	
	public static void main(String []args) {

		List<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(2);
		list.add(3);
		for(int i=0; i < list.size(); i++) {
			list.remove(i);
			i--;
		}
	}
	
}
